/****************************************************************************
 Copyright (c) 2017-2018 SuperSuRaccoon
 
 Site: http://www.supersuraccoon-cocos2d.com
 Mail: supersuraccoon@gmail.com

 Permission is hereby granted, free of charge, to any person obtaining a copy
 of this software and associated documentation files (the "Software"), to deal
 in the Software without restriction, including without limitation the rights
 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 copies of the Software, and to permit persons to whom the Software is
 furnished to do so, subject to the following conditions:

 The above copyright notice and this permission notice shall be included in
 all copies or substantial portions of the Software.

 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 THE SOFTWARE.
 ****************************************************************************/

const ssr = require('../namespace/SVGNamespace');

ssr.SVG.Util.Polyline = function() {};

ssr.SVG.Util.Polyline.FromCubicBezier = function(px, py, cx1, cy1, cx2, cy2, x, y, segments) {
	let outputArray = [];
	let t = 1.0 / segments;
    for (let i = t; i < 1; i += t) {
        let xx = Math.pow(1 - i, 3) * px + 3.0 * Math.pow(1 - i, 2) * i * cx1 + 3.0 * (1 - i) * i * i * cx2 + i * i * i * x;
        let yy = Math.pow(1 - i, 3) * py + 3.0 * Math.pow(1 - i, 2) * i * cy1 + 3.0 * (1 - i) * i * i * cy2 + i * i * i * y;
        outputArray.push({"x": parseFloat(xx.toFixed(3)), "y": parseFloat(yy.toFixed(3))});
    }
    return outputArray;
};

ssr.SVG.Util.Polyline.FromQuadBezier = function(px, py, cx1, cy1, x, y, segments) {
	let outputArray = [];
	let t = 1.0 / segments;
    for (let i = t; i < 1; i += t) {
        let xx = Math.pow(1 - i, 2) * px + 2.0 * (1 - i) * i * cx1 + i * i * x;
        let yy = Math.pow(1 - i, 2) * py + 2.0 * (1 - i) * i * cy1 + i * i * y;
        outputArray.push({"x": parseFloat(xx.toFixed(3)), "y": parseFloat(yy.toFixed(3))});
    }
    return outputArray;
};

ssr.SVG.Util.Polyline.FromEllipticArc = function(x1, y1, rx, ry, xAxisRotation, largeArcFlag, sweepFlag, x2, y2, segments) {
	var phi = cc.misc.degreesToRadians(xAxisRotation % 360);
	var cosPhi = Math.cos(phi);
	var sinPhi = Math.sin(phi);
	var halfDifX = (x1 - x2) / 2;
	var halfDifY = (y1 - y2) / 2;
	var x1p =  cosPhi * halfDifX + sinPhi * halfDifY;
	var y1p = -sinPhi * halfDifX + cosPhi * halfDifY;
	var prx, pry, px1p, py1p, radiiCheck, sign, denominator, numerator, coef, cxp, cyp, halfSumX, halfSumY, cx, cy, ux, uy, vx, vy, angleStart, angleExtent;
	rx = Math.abs(rx);
	ry = Math.abs(ry);
	prx = rx * rx;
	pry = ry * ry;
	px1p = x1p * x1p;
	py1p = y1p * y1p;
  	// check that radii are large enough
  	radiiCheck = px1p / prx + py1p / pry;
  	if (radiiCheck > 1) {
    	rx *= Math.sqrt(radiiCheck);
    	ry *= Math.sqrt(radiiCheck);
    	prx = rx * rx;
    	pry = ry * ry;
  	}
	// compute: (cx1, cy1)
	var sign = largeArcFlag === sweepFlag ? -1 : 1;
	var denominator = prx * py1p + pry * px1p;
	var numerator = prx * pry - denominator;
	var coef = numerator < 0 ? 0 : sign * Math.sqrt(numerator / denominator);

	var cxp = coef * (rx * y1p / ry);
	var cyp = coef * (-ry * x1p / rx);

	var halfSumX = (x1 + x2) / 2;
	var halfSumY = (y1 + y2) / 2;
	var cx = cosPhi * cxp - sinPhi * cyp + halfSumX;
	var cy = sinPhi * cxp + cosPhi * cyp + halfSumY;

	var ux = (x1p - cxp) / rx;
	var uy = (y1p - cyp) / ry;
	var vx = (-x1p - cxp) / rx;
	var vy = (-y1p - cyp) / ry;

	sign = uy < 0 ? -1 : 1;
	numerator = ux;
	denominator = Math.sqrt(ux * ux + uy * uy);
	var angleStart = cc.misc.radiansToDegrees(sign * Math.acos(numerator / denominator));

	sign = ux * vy - uy * vx < 0 ? -1 : 1;
	numerator = ux * vx + uy * vy;
	denominator = Math.sqrt((ux * ux + uy * uy) * (vx * vx + vy * vy));
	var angleExtent = cc.misc.radiansToDegrees(sign * Math.acos(numerator / denominator));

	if (!sweepFlag && angleExtent > 0) {
		angleExtent -= 360;
	} 
	else if (sweepFlag && angleExtent < 0) {
		angleExtent += 360;
	}
	angleExtent %= 360;
	angleStart %= 360;
	//
	let outputArray = [];
	let t = 0;
    for (let s = 1; s < segments; s++) {
        t = s / segments;
        var theta = cc.misc.degreesToRadians(angleStart + t * angleExtent);
	  	var rxc = rx * Math.cos(theta);
		var rys = ry * Math.sin(theta);
		outputArray.push({"x": (cx + cosPhi * rxc - sinPhi * rys), "y": (cy + sinPhi * rxc + cosPhi * rys)});
    }
    outputArray.push({"x": parseFloat(x2.toFixed(3)), "y": parseFloat(y2.toFixed(3))});
  	return outputArray;
};

ssr.SVG.Util.Polyline.FromLine = function(x1, y1, x2, y2, segments) {
	let outputArray = [];
	let t = 1 / segments;
	for (var i = t; i < 1; i += t) {
		let out = cc.v2(0, 0);
		cc.Vec2.lerp(out, cc.v2(x1, y1), {"x": parseFloat(x2.toFixed(3)), "y": parseFloat(y2.toFixed(3))}, i);
	    outputArray.push(out.x, out.y);
	}
    return outputArray;
};

ssr.SVG.Util.Polyline.FromMedians = function(x1, y1, x2, y2, segments) {
	let outputArray = [];
	if (segments == 1) {
		outputArray.push([x1, y1]);
		outputArray.push([x2, y2]);
		return outputArray;
	}
	let t = 1 / segments;
	for (var i = 0; i <= 1; i += t) {
		let out = cc.v2(0, 0);
		cc.Vec2.lerp(out, cc.v2(x1, y1), {"x": parseFloat(x2.toFixed(3)), "y": parseFloat(y2.toFixed(3))}, i);
	    outputArray.push([parseFloat(out.x.toFixed(3)), parseFloat(out.y.toFixed(3))]);
	}
    return outputArray;
};
